<?php

namespace App\Admin\Controllers;

use App\Models\PowerLog;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PowerLogController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('算力变更记录')
            ->description('算力变更记录')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PowerLog);

        $grid->id('Id');
        // $grid->user_id('User id');
        $grid->user()->nickname('用户昵称');
        $grid->powertype()->name('类型');
        $grid->power('数值');
        $grid->status('状态')->display(function($value){
            return $value==1?"<span class='label label-success'>增加</span>":"<span class='label label-warning'>减少</span>";
        });
        $grid->comment('备注');
        $grid->created_at('创建时间');

        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->scope('增加')->where('status', 1);
            $filter->scope('减少')->where('status', 2);
            $filter->where(function ($query){
                    $query->whereHas('user',function ($query){
                        $query->where('nickname','like','%'.$this->input.'%');
                    });
                },'用户昵称');
            $filter->between('created_at', '变动时间')->datetime();
        });
        $grid->actions(function($actions){
            
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PowerLog::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->phone('Phone');
        $show->type('Type');
        $show->power('Power');
        $show->status('Status');
        $show->comment('Comment');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PowerLog);

        $form->number('user_id', 'User id');
        $form->mobile('phone', 'Phone');
        $form->number('type', 'Type');
        $form->decimal('power', 'Power');
        $form->switch('status', 'Status');
        $form->text('comment', 'Comment');

        return $form;
    }
}
